package com.qiqijuice.sales;

import net.miginfocom.swt.MigLayout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class ConfirmationDialog {
	private static int WIDTH = 500;
	private static int HEIGHT = 500;
	
	private Shell shell;
	private boolean confirmed;
	private Display display;
	
	private String title;
	
	public ConfirmationDialog(String title, Display display) {
		this.display = display;
		this.title = title;
		confirmed = false;
		
		shell = new Shell(display, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		
		shell.setSize(WIDTH, HEIGHT);
		shell.setText("Confirmation!");
		
		Rectangle r = display.getBounds();
		int x = (r.width - shell.getSize().x) / 2; 
		int y = (r.height - shell.getSize().y) / 2;
		shell.setLocation(x,y);
		
		MigLayout layout = new MigLayout("debug, fill", "");
		shell.setLayout(layout);
		
		Label l = new Label(shell, SWT.None);
		l.setText(title);
		l.setLayoutData("north, height 40");
		
		Button b = new Button(shell, SWT.PUSH);
		b.setText("Yes");
		b.setLayoutData("align right, width 150, height 150, sg 1");
		b.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				confirmed = true;
				shell.dispose();
			}
			
			
		});
		
		b = new Button(shell, SWT.PUSH);
		b.setText("No");
		b.setLayoutData("sg 1");
		b.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				confirmed = false;
				shell.dispose();
			}
			
			
		});
	}
	
	public boolean open() {
		confirmed = false;
		shell.open();
		while(!shell.isDisposed()) {
			if (display.readAndDispatch())
				display.sleep();
		}
		
		return confirmed;
	}

	public static void main(String[] args) {
		Display display = new Display();
		
		ConfirmationDialog d = new ConfirmationDialog("Test confirmation", display);
		d.open();
		
		display.dispose();
	}
}

